<template>
	<view class="content">
		<!-- 搜索 -->
		<view class="uni-search-box">
			<uni-search-bar ref="searchBar" v-model="searchText" :placeholder="inputPlaceholder" radius="100" disabled
				cancelButton="none" />
			<view class="cover-search-bar" @click="searchClick"></view>
		</view>
		<unicloud-db ref="udb" v-slot:default="{data, loading, error, options}" collection="opendb-mall-goods"
			loadtime="onready" :where="where">
			<view v-if="error">{{error.message}}</view>
			<view v-else>
				<view v-show="data.length==0" style="margin-top: 200rpx;">
					<image style="width: 750rpx;" :src="'https://s1.ax1x.com/2022/08/01/vAIOr4.png'" mode="aspectFit">
					</image>
				</view>
				<view class="goods-box" v-for='item in data'>
					<uni-row class="row">
						<uni-col :span="8">
							<view @click="clickGoods(item._id)">
								<image class="goods-img" :src="item.goods_thumb.path"></image>
							</view>
						</uni-col>
						<uni-col :span="16">
							<view class="goods-desc-box" @click="clickGoods(item._id)">
								<p>
									{{item.name}}
								</p>
								<text>
									{{item.goods_desc}}
								</text>
							</view>
							<view class="price-box">
								<text class="price-font">
									<text class="yuan-font">￥</text>
									<text style=" font-size: 40rpx;">
										{{item.goods_price}}
									</text>
								</text>
							</view>
							<uni-icons type="cart" size="30" class="addCart" @click="addShoppingCart(item._id)"
								color="#FF5000"></uni-icons>
						</uni-col>
					</uni-row>
				</view>
			</view>
		</unicloud-db>
	</view>
</template>

<script>
	import searchListVue from './search-list.vue'
	export default {
		data() {
			return {
				searchText: '',
				inputPlaceholder: '搜索'
			}
		},
		onLoad(option) {
			this.searchText = option.searchText
		},
		onReady() {
			this.$refs.udb.refresh()
		},
		computed: {
			where() {
				return `${new RegExp(this.searchText, 'i')}.test(name)` // 使用计算属性得到完整where
			}
		},
		methods: {
			getLastDate(lastDate) {
				this.lastDate = parseInt(lastDate)
			},
			async clickGoods(id) {
				let res = await uniCloud.importObject('goods').getById(id)
				uni.navigateTo({
					url: '/pages/details/details?item=' + encodeURIComponent(JSON.stringify(res[0]))
				})
			},
			async getGoods() {
				let res = await uniCloud.importObject('goods').getGoodsBySearch(this.searchText)
				if (res.length == 0) {
					uni.showToast({
						title: '没有了哦~',
						icon: "loading",
						duration: 800
					})
					return;
				}
				console.log('--------');
				console.log(res[0].name);
				this.lastDate = res[res.length - 1].auto_createdate

				this.goodsList = [...this.goodsList, ...res]
			},
			//添加购物车
			addShoppingCart(id) {
				const db = uniCloud.database();
				db.collection('shop-cart').add({
					'goods_id': id
				}).then(res => {
					uni.showToast({
						title: '添加成功！',
						icon: 'success',
						duration: 800
					})
				}).catch(e => {
					uni.showToast({
						title: '不能重复添加！',
						icon: 'error',
						duration: 800
					})
				})
			},
			//点击搜索框
			searchClick(e) {
				uni.hideKeyboard();
				uni.redirectTo({
					url: '/pages/list/search/search',
					animationType: 'fade-in'
				});
			},
		}
	}
</script>

<style>
	/* 商品区 */
	.goods-box {
		width: 710rpx;
		margin: 10px auto 0rpx;
	}

	.goods-desc-box {
		height: 70%;
		width: 85%;
		overflow: hidden;
		text-overflow: ellipsis;
	}

	.goods-desc-box text {
		font-size: 12px;
		color: #a2a2a2;
	}

	.goods-img {
		height: 200rpx;
		width: 200rpx;
		border-radius: 8px
	}

	.row {
		height: 200rpx;
		border-radius: 18rpx;
		background-color: #ffffff;
	}

	.yuan-font {
		color: #FF5000;
		font-size: 32rpx;
	}

	.price-box {
		height: 30%;
		width: 85%;
		word-wrap: break-word;
		position: absolute;
		bottom: 0rpx;
	}

	.x1 {
		font-size: 32rpx;
		margin: 10rpx;
		width: 50rpx;
		height: 50rpx;
		border: #8a8a8a 1px solid;
		border-radius: 18rpx;
		display: inline-block;
		line-height: 50rpx;
		text-align: center;
	}

	.addCart {
		height: 60rpx;
		width: 60rpx;
		position: absolute;
		bottom: 10rpx;
		right: 10rpx;
	}


	/* 搜索 */
	.uni-search-box {
		background-color: #FFFFFF;
		position: sticky;
		height: 50px;
		top: 0;
		left: 0;
		/* #ifndef APP-PLUS */
		z-index: 9;
		/* #endif */
	}

	.cover-search-bar {
		height: 50px;
		position: relative;
		top: -50px;
		margin-bottom: -50px;
		/* #ifndef APP-NVUE */
		z-index: 999;
		/* #endif */
	}
</style>